﻿Imports MongoDB.Driver
Imports MongoDB.Driver.Builders

Public Class modShared

    Public Const mcstrFacebookAppID As String = "427588657269951"
    Public Const mcstrFacebookAppSecret As String = "ee1d6723fa94695fa857a9b6915fca25"

    Public Const mintDefaultMaxMapPlayers As Integer = 6
    Public Const mintDefaultMapBonusDivider As Integer = 3
    Public Shared mlintPlayerCount As List(Of Integer) = Enumerable.Range(2, 7).ToList()

    Public Shared Function UserLogin(ByVal strEmail As String, _
                                     ByVal strPassword As String, _
                                     ByRef mdMessageDisplay As Trebuchet.MessageDisplay) As Boolean

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUsers As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mcUserCursor As MongoCursor = Nothing
        Dim uUser As Trebuchet.User = Nothing
        Dim bolLoggedIn As Boolean = False

        Try

            mdDatabase = DBHelper.GetDataBase()
            mcUsers = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            'get all users that match the username and are active
            mcUserCursor = mcUsers.FindAs(Of Trebuchet.User)(Query.And(Query.EQ("Email", strEmail.Trim), Query.EQ("Active", True)))

            If Not mcUserCursor Is Nothing _
                AndAlso mcUserCursor.Count > 0 Then

                For Each uUser In mcUserCursor

                    If strPassword.Trim = Encryption.AES_Decrypt(uUser.Password, Encryption.strSharedAESPassword) Then

                        'Log user in
                        HttpContext.Current.Session("User_id") = uUser._id
                        HttpContext.Current.Session("Email") = uUser.Email

                        bolLoggedIn = True

                    Else

                        mdMessageDisplay.AddMessage("Incorrect Password")
                        bolLoggedIn = False

                    End If

                Next

            Else

                mdMessageDisplay.AddMessage("User not Found")
                bolLoggedIn = False

            End If

        Catch ex As Exception

            mdMessageDisplay.AddMessage(ex.Message)

        Finally

            mdDatabase = Nothing
            mcUsers = Nothing
            mcUserCursor = Nothing
            uUser = Nothing

            UserLogin = bolLoggedIn

        End Try

    End Function

    Public Shared Function UserRegister(ByVal strEmail As String, _
                                     ByVal strPassword As String, _
                                     ByRef mdMessageDisplay As Trebuchet.MessageDisplay) As Boolean

        Dim mdDatabase As MongoDatabase = Nothing
        Dim mcUsers As MongoCollection(Of Trebuchet.User) = Nothing
        Dim mcUserCursor As MongoCursor = Nothing
        Dim uUser As Trebuchet.User = Nothing
        Dim bolSave As Boolean = True

        Try

            'Check to see if the user or email already exists in the system
            mdDatabase = DBHelper.GetDataBase()
            mcUsers = mdDatabase.GetCollection(Of Trebuchet.User)("Users")

            mcUserCursor = mcUsers.FindAs(Of Trebuchet.User)(Query.And(Query.EQ("Email", strEmail.Trim), Query.EQ("Active", True)))

            If Not mcUserCursor Is Nothing _
                AndAlso mcUserCursor.Count > 0 Then

                bolSave = False

                'Username already exists, display message
                mdMessageDisplay.AddMessage("Email already in use.")

            End If

            If bolSave Then

                'Save new user
                uUser = New Trebuchet.User

                uUser.Email = strEmail.Trim
                uUser.Password = Encryption.AES_Encrypt(strPassword.Trim, Encryption.strSharedAESPassword)

                mcUsers.Save(uUser)

                'Log user in
                HttpContext.Current.Session("User_id") = uUser._id
                HttpContext.Current.Session("Email") = uUser.Email

            End If

        Catch ex As Exception

            mdMessageDisplay.AddMessage(ex.Message)

        Finally

            mdDatabase = Nothing
            mcUsers = Nothing
            mcUserCursor = Nothing
            uUser = Nothing

            UserRegister = bolSave

        End Try

    End Function

End Class
